package demo;

import java.util.HashSet;
import java.util.Iterator;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import cn.wps.moffice.main.local.filebrowser.BrowserConst;
import cn.wps.moffice.main.local.filebrowser.logic.SeekCallbackThread;
import cn.wps.moffice.main.local.filebrowser.logic.SeekCallbackThread.Callback;
import cn.wps.moffice.main.local.filebrowser.operator.fscache.DataCenter;
import cn.wps.moffice.main.local.filebrowser.operator.seek.FsFinder;
import cn.wps.moffice.main.local.filebrowser.operator.seek.IFinder.SeekCallback;
import cn.wps.moffice.main.local.filebrowser.operator.seek.IFinder.SeekType;

import com.example.localfiledata.R;

public class MainActivity extends Activity {

	private TextView mAllResult;
	private TextView mDocResult;
	private TextView mPptResult;
	private TextView mXlsResult;
	private TextView mPdfResult;
	private TextView mTxtResult;
	private TextView mOtherResult;
	private String TAG = "AIDLSERVER ===>";

	private int mCurrentType = BrowserConst.ALL_POSITION;

	private SeekCallbackThread mThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mAllResult = (TextView) findViewById(R.id.all_result);
		mDocResult = (TextView) findViewById(R.id.doc_result);
		mPptResult = (TextView) findViewById(R.id.ppt_result);
		mXlsResult = (TextView) findViewById(R.id.xls_result);
		mPdfResult = (TextView) findViewById(R.id.pdf_result);
		mTxtResult = (TextView) findViewById(R.id.txt_result);
		mOtherResult = (TextView) findViewById(R.id.other_result);
		initSeekCallbackThread();
		
	}

	/**
	 * 下拉刷新
	 * 
	 * @param v
	 */
	public void onPullRefreshClick(View v) {
		mCurrentType = Integer.parseInt((String) v.getTag());
		FsFinder.getInstance().find(mSeek);
	}

	/**
	 * 滑动切换
	 * 
	 * @param v
	 */
	public void onSlipRefreshClick(View v) {
		mCurrentType = Integer.parseInt((String) v.getTag());
		mSeek.updateLastState(SeekType.OnFresh);
	}

	private TextView getByType(int type) {
		switch (type) {
		case BrowserConst.ALL_POSITION:
			return mAllResult;
		case BrowserConst.DOC_POSITION:
			return mDocResult;
		case BrowserConst.PPT_POSITION:
			return mPptResult;
		case BrowserConst.XSL_POSITION:
			return mXlsResult;
		case BrowserConst.PDF_POSITION:
			return mPdfResult;
		case BrowserConst.TXT_POSITION:
			return mTxtResult;
		case BrowserConst.OTHER_POSITION:
			return mOtherResult;

		default:
			break;
		}
		return mAllResult;
	}

	private void initSeekCallbackThread() {
		mThread = new SeekCallbackThread(
				new Callback() {

			@Override
			public void refresh(SeekType seekType, int dirtyFlag, boolean isFinished) {
				boolean isDirty = DataCenter.isDirty(dirtyFlag, mCurrentType);
				final TextView textView = getByType(mCurrentType);
				if (!isDirty) {//没有新数据加入
					if (isFinished) {
						if (!TextUtils.isEmpty(textView.getText())) {
							return;
						}
					} else {
						return;
					}
				}

				HashSet<String> rs = DataCenter.getInstance().getScanSet(mCurrentType);//从数据中心得到数据
				Iterator<String> itor = rs.iterator();
				final StringBuilder sf = new StringBuilder();
				while (itor.hasNext()) {
					sf.append(itor.next() + "\n");
				}
				
				runOnUiThread(new Runnable() {
					public void run() {
						textView.setText(sf);
					}
				});
			}
		});
	}

	private SeekCallback mSeek = new SeekCallback() {

		@Override
		public void updateLastState(SeekType seekType) {
			mThread.refresh(seekType, Integer.MAX_VALUE, mThread.isFinished());
		}

		@Override
		public void seekFinish(SeekType seekType, int dirtyFlag) {
			mThread.refresh(seekType, dirtyFlag, true);
		}

		@Override
		public void refresh(SeekType seekType, int dirtyFlag) {
			mThread.refresh(seekType, dirtyFlag, false);
		}

		@Override
		public void dispose() {
			mThread.dispose();
		}
	};

}
